<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绿能充 - 故障报告管理</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --primary-color: #00a896;
            --secondary-color: #02c39a;
            --dark-color: #05668d;
            --light-color: #f0f3bd;
            --accent-color: #028090;
            --text-dark: #333;
            --text-light: #f8f9fa;
            --shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            color: var(--text-dark);
            background-color: #f9f9f9;
            min-height: 100vh;
            display: flex;
        }

        /* 侧边栏样式 - 保持不变 */
        .sidebar {
            width: 250px;
            background: linear-gradient(135deg, var(--dark-color) 0%, var(--primary-color) 100%);
            color: white;
            padding: 20px 0;
            height: 100vh;
            position: fixed;
            box-shadow: var(--shadow);
            z-index: 100;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .sidebar-header .logo {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .sidebar-header .logo-icon {
            width: 40px;
            height: 40px;
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 18px;
            color: var(--light-color);
        }

        .sidebar-header h3 {
            font-size: 1.2rem;
            font-weight: 500;
            margin: 0;
        }

        .sidebar-header p {
            font-size: 0.8rem;
            opacity: 0.8;
            margin: 0;
        }

        .nav-menu {
            list-style: none;
            padding: 0 15px;
        }

        .nav-item {
            margin-bottom: 5px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            border-radius: 8px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
        }

        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 16px;
        }

        .nav-link .badge {
            margin-left: auto;
            background-color: var(--light-color);
            color: var(--dark-color);
            font-size: 0.7rem;
            padding: 3px 6px;
        }

        .user-profile {
            position: absolute;
            bottom: 20px;
            left: 0;
            width: 100%;
            padding: 0 20px;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 10px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            cursor: pointer;
            transition: var(--transition);
        }

        .user-info:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }

        .user-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.2);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 14px;
        }

        .user-name {
            font-size: 0.9rem;
            font-weight: 500;
            margin-bottom: 2px;
        }

        .user-role {
            font-size: 0.7rem;
            opacity: 0.8;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 30px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title h1 {
            font-size: 1.8rem;
            font-weight: 600;
            color: var(--dark-color);
            margin-bottom: 5px;
        }

        .page-title p {
            color: #777;
            font-size: 0.9rem;
            margin: 0;
        }

        .header-actions {
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 500;
            border: none;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
        }

        .btn i {
            margin-right: 6px;
        }

        .btn-primary {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            box-shadow: 0 4px 15px rgba(0, 168, 150, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 168, 150, 0.4);
        }

        .btn-outline {
            background: transparent;
            border: 1px solid #ddd;
            color: #555;
        }

        .btn-outline:hover {
            background-color: #f5f5f5;
        }

        .btn-danger {
            background: linear-gradient(to right, #f44336, #ff9800);
            color: white;
        }

        .btn-warning {
            background: linear-gradient(to right, #ff9800, #ffc107);
            color: white;
        }

        .btn-sm {
            padding: 5px 10px;
            font-size: 0.8rem;
        }

        /* 搜索和过滤区域 */
        .filter-container {
            background-color: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: var(--shadow);
            margin-bottom: 20px;
        }

        .filter-row {
            display: flex;
            gap: 15px;
            margin-bottom: 15px;
        }

        .filter-group {
            flex: 1;
        }

        .filter-label {
            display: block;
            margin-bottom: 5px;
            font-size: 0.9rem;
            color: #555;
        }

        .filter-input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 0.9rem;
        }

        .filter-select {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 0.9rem;
            background-color: white;
        }

        .filter-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            padding-top: 10px;
        }

        /* 表格样式 */
        .card {
            background-color: white;
            border-radius: 12px;
            box-shadow: var(--shadow);
            padding: 20px;
            margin-bottom: 20px;
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .card-header h3 {
            font-size: 1.2rem;
            font-weight: 600;
            margin: 0;
            color: var(--dark-color);
        }

        .table-responsive {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            font-weight: 500;
            color: #555;
            font-size: 0.9rem;
        }

        tr:hover {
            background-color: #f9f9f9;
        }

        .badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .badge-reported {
            background-color: #fff3e0;
            color: #ff9800;
        }

        .badge-processing {
            background-color: #e3f2fd;
            color: #2196f3;
        }

        .badge-resolved {
            background-color: #e8f5e9;
            color: #4caf50;
        }

        /* 分页样式 */
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }

        .page-item {
            margin: 0 5px;
        }

        .page-link {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            color: #555;
            text-decoration: none;
            transition: var(--transition);
        }

        .page-link:hover {
            background-color: #f5f5f5;
        }

        .page-item.active .page-link {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            border-color: var(--primary-color);
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                overflow: hidden;
            }

            .sidebar-header h3,
            .sidebar-header p,
            .nav-link span,
            .user-name,
            .user-role {
                display: none;
            }

            .sidebar-header {
                padding: 15px 10px;
            }

            .nav-link {
                justify-content: center;
                padding: 12px 0;
            }

            .nav-link i {
                margin-right: 0;
                font-size: 18px;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .filter-row {
                flex-direction: column;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
        }

        @media (max-width: 576px) {
            .main-content {
                padding: 20px 15px;
            }

            .btn {
                padding: 6px 10px;
                font-size: 0.8rem;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航栏 - 保持不变 -->
<div class="sidebar">
    <div class="sidebar-header">
        <div class="logo">
            <div class="logo-icon">
                <i class="fas fa-charging-station"></i>
            </div>
            <div>
                <h3>绿能充</h3>
                <p>管理员面板</p>
            </div>
        </div>
    </div>

    <ul class="nav-menu">
        <li class="nav-item">
            <a href="/admin/dashboard" class="nav-link">
                <i class="fas fa-tachometer-alt"></i>
                <span>仪表盘</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/user/list" class="nav-link">
                <i class="fas fa-users"></i>
                <span>用户管理</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/pile/list" class="nav-link">
                <i class="fas fa-charging-station"></i>
                <span>充电桩管理</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/report/list" class="nav-link">
                <i class="fas fa-list-alt"></i>
                <span>充电记录</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/fault/list" class="nav-link active">
                <i class="fas fa-exclamation-triangle"></i>
                <span>故障报告</span>
                <span class="badge">${pendingCount}</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/settings" class="nav-link">
                <i class="fas fa-cog"></i>
                <span>系统设置</span>
            </a>
        </li>
    </ul>

    <div class="user-profile">
        <div class="user-info">
            <div class="user-avatar">
                <i class="fas fa-user"></i>
            </div>
            <div>
                <div class="user-name">管理员</div>
                <div class="user-role">超级管理员</div>
            </div>
        </div>
    </div>
</div>

<!-- 主内容区 -->
<div class="main-content">
    <div class="header">
        <div class="page-title">
            <h1>故障报告管理</h1>
            <p>查看和处理系统故障报告</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-primary">
                <a href="${pageContext.request.contextPath}/admin/fault/add">
                    <i class="fas fa-plus"></i> 新增报告
                </a>
            </button>
            <button class="btn btn-outline">
                <i class="fas fa-download"></i> 导出
            </button>
        </div>
    </div>

    <!-- 搜索和过滤区域 -->
    <div class="filter-container">
        <form action="/admin/fault/list" method="get">
            <div class="filter-row">
                <div class="filter-group">
                    <label class="filter-label">充电桩编号</label>
                    <input type="text" name="pileCode" class="filter-input" placeholder="输入充电桩编号" value="${param.pileCode}">
                </div>
                <div class="filter-group">
                    <label class="filter-label">报告人</label>
                    <input type="text" name="reporter" class="filter-input" placeholder="输入报告人姓名" value="${param.reporter}">
                </div>
                <div class="filter-group">
                    <label class="filter-label">故障类型</label>
                    <select name="faultType" class="filter-select">
                        <option value="">全部类型</option>
                        <option value="POWER" ${param.faultType == 'POWER' ? 'selected' : ''}>电源故障</option>
                        <option value="NETWORK" ${param.faultType == 'NETWORK' ? 'selected' : ''}>网络故障</option>
                        <option value="SCREEN" ${param.faultType == 'SCREEN' ? 'selected' : ''}>屏幕故障</option>
                        <option value="OTHER" ${param.faultType == 'OTHER' ? 'selected' : ''}>其他故障</option>
                    </select>
                </div>
            </div>
            <div class="filter-row">
                <div class="filter-group">
                    <label class="filter-label">状态</label>
                    <select name="status" class="filter-select">
                        <option value="">全部状态</option>
                        <option value="REPORTED" ${param.status == 'REPORTED' ? 'selected' : ''}>已报告</option>
                        <option value="PROCESSING" ${param.status == 'PROCESSING' ? 'selected' : ''}>处理中</option>
                        <option value="RESOLVED" ${param.status == 'RESOLVED' ? 'selected' : ''}>已解决</option>
                    </select>
                </div>
                <div class="filter-group">
                    <label class="filter-label">报告时间</label>
                    <input type="date" name="startDate" class="filter-input" value="${param.startDate}">
                </div>
                <div class="filter-group">
                    <label class="filter-label">至</label>
                    <input type="date" name="endDate" class="filter-input" value="${param.endDate}">
                </div>
            </div>
            <div class="filter-actions">
                <button type="reset" class="btn btn-outline">重置</button>
                <button type="submit" class="btn btn-primary">搜索</button>
            </div>
        </form>
    </div>

    <!-- 故障报告表格 -->
    <div class="card">
        <div class="card-header">
            <h3>故障报告列表</h3>
            <div>
                <span class="filter-label">共 ${totalItems} 条记录</span>
            </div>
        </div>
        <div class="table-responsive">
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>充电桩</th>
                    <th>故障类型</th>
                    <th>报告人</th>
                    <th>报告时间</th>
                    <th>状态</th>
                    <th>处理人</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${faults}" var="fault">
                    <tr>
                        <td>${fault.id}</td>
                        <td>${fault.pile.code}</td>
                        <td>
                            <c:choose>
                                <c:when test="${fault.faultType == 'POWER'}">电源故障</c:when>
                                <c:when test="${fault.faultType == 'NETWORK'}">网络故障</c:when>
                                <c:when test="${fault.faultType == 'SCREEN'}">屏幕故障</c:when>
                                <c:when test="${fault.faultType == 'OTHER'}">其他故障</c:when>
                            </c:choose>
                        </td>
                        <td>${fault.reporter.username}</td>
                        <td><fmt:formatDate value="${fault.createTime}" pattern="yyyy-MM-dd HH:mm" /></td>
                        <td>
                            <c:choose>
                                <c:when test="${fault.status == 'REPORTED'}">
                                    <span class="badge badge-reported">已报告</span>
                                </c:when>
                                <c:when test="${fault.status == 'PROCESSING'}">
                                    <span class="badge badge-processing">处理中</span>
                                </c:when>
                                <c:when test="${fault.status == 'RESOLVED'}">
                                    <span class="badge badge-resolved">已解决</span>
                                </c:when>
                            </c:choose>
                        </td>
                        <td>
                            <c:if test="${not empty fault.handler}">
                                ${fault.handler.username}
                            </c:if>
                        </td>
                        <td>
                            <div style="display: flex; gap: 5px;">
                                <button class="btn btn-outline btn-sm"
                                        onclick="location.href='/admin/fault/detail?id=${fault.id}'">
                                    <i class="fas fa-eye"></i> 详情
                                </button>
                                <c:if test="${fault.status != 'RESOLVED'}">
                                    <button class="btn btn-warning btn-sm"
                                            onclick="handleFault(${fault.id}, 'PROCESSING')">
                                        <i class="fas fa-tools"></i> 处理
                                    </button>
                                    <button class="btn btn-danger btn-sm"
                                            onclick="handleFault(${fault.id}, 'RESOLVED')">
                                        <i class="fas fa-check"></i> 解决
                                    </button>
                                </c:if>
                            </div>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>

        <!-- 分页 -->
        <div class="pagination">
            <c:if test="${currentPage > 1}">
                <a href="?page=${currentPage - 1}&pileCode=${param.pileCode}&reporter=${param.reporter}&faultType=${param.faultType}&status=${param.status}&startDate=${param.startDate}&endDate=${param.endDate}"
                   class="page-link">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </c:if>

            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <span class="page-item active">
                            <a href="#" class="page-link">${i}</a>
                        </span>
                    </c:when>
                    <c:otherwise>
                        <span class="page-item">
                            <a href="?page=${i}&pileCode=${param.pileCode}&reporter=${param.reporter}&faultType=${param.faultType}&status=${param.status}&startDate=${param.startDate}&endDate=${param.endDate}"
                               class="page-link">${i}</a>
                        </span>
                    </c:otherwise>
                </c:choose>
            </c:forEach>

            <c:if test="${currentPage < totalPages}">
                <a href="?page=${currentPage + 1}&pileCode=${param.pileCode}&reporter=${param.reporter}&faultType=${param.faultType}&status=${param.status}&startDate=${param.startDate}&endDate=${param.endDate}"
                   class="page-link">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </c:if>
        </div>
    </div>
</div>

<script>
    // 处理故障状态变更
    function handleFault(faultId, status) {
        if (confirm(status === 'PROCESSING' ? '确定要开始处理此故障吗？' : '确定此故障已解决吗？')) {
            fetch('/admin/fault/handle', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `id=${faultId}&status=${status}`
            })
                .then(response => {
                    if (response.ok) {
                        location.reload();
                    } else {
                        alert('操作失败，请重试');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('操作失败，请重试');
                });
        }
    }
</script>
</body>
</html>